package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.UploadUtil;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService {
    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private DailyContentMapper dailyContentMapper;
    @Autowired
    private CommentMapper commentMapper;//用于查询评论数

    @Override
    public PageInfo<Daily> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Daily> list = dailyMapper.queryForList(qo);
        return new PageInfo<>(list);
    }


    @Override
    public Daily selectById(Long id) {
        Daily daily = dailyMapper.selectByPrimaryKey(id);
        //查询评论数
        daily.setCommentNum(commentMapper.selectForNum(id, Comment.MODELSTATE_DAILY));
        //查询点赞数量
        int likeNum = dailyMapper.queryLikeCountByDailyId(id);
        //设置点赞数量
        daily.setLikeNum(likeNum);
        //查询当前用户是否已经点赞
        //获取当前用户的id
        if (UserContext.getCurrentUser()!=null){
            Long userId = UserContext.getCurrentUser().getId();
            //查询当前用户是否已点赞
            int isLike = dailyMapper.queryIsLike(userId, id);
            //处理查询结果
            if (isLike==1){
                daily.setLike(true);
            }else {
                daily.setLike(false);
            }
        }

        return daily;

    }

    @Override
    public DailyContent selectContentById(Long id) {
        return dailyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Daily daily, MultipartFile file) {
        if (file != null) {
            String url = UploadUtil.uploadQiniu(file);
            daily.setCoverUrl(url);
        }
        if(daily.getSequence()==1){//保证只能有一条热门日报
            Daily hotDaily = dailyMapper.selectHotDaily(1);
            if (hotDaily!=null){
                dailyMapper.updateHotDailyById(hotDaily.getId());
            }
        }
        DailyContent dailyContent = daily.getDailyContent();
        if (daily.getState() == 1) {
            daily.setReleaseTime(new Date());
        }
        if (daily.getId() == null) {//保存日报
            dailyMapper.insert(daily);
            if (dailyContent != null) {
                dailyContent.setId(daily.getId());
                dailyContent.setContent(daily.getDailyContent().getContent());
                dailyContentMapper.insert(dailyContent);
            }else{
                DailyContent dailyContent1=new DailyContent();
                dailyContentMapper.insert(dailyContent1);
            }

        } else {
            dailyMapper.updateByPrimaryKey(daily);//更改日报
            if (dailyContent != null) {//内容不为空,则修改内容
                dailyContent.setId(daily.getId());
                dailyContent.setContent(daily.getDailyContent().getContent());
                dailyContentMapper.updateByPrimaryKey(dailyContent);
            }else{//内容为空,则创建一个内容对象,并保存内容
                DailyContent dailyContent1=new DailyContent();
                dailyContentMapper.insert(dailyContent1);
            }
        }

    }

    @Override
    public PageInfo<Daily> queryForApp(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Daily> list=dailyMapper.queryForApp(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Daily selectHotDaily(Integer sequence) {
        return dailyMapper.selectHotDaily(sequence);
    }

    @Override
    public void updateBrowseNumById(Long id) {
        dailyMapper.updateBrowseNumById(id);
    }

    @Override
    public int queryForCount(SearchQueryObject qo) {
        return dailyMapper.queryForCount(qo);
    }
}
